﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Pyramid.Models;

namespace Pyramid.Controllers
{
	public class StaffPositionController : BaseController
	{
		public ActionResult List()
		{
			var model = Db.StaffPositions.ToList();
			return View(model);
		}

		public ActionResult Create()
		{
			var model = new StaffPosition();
			return View("Edit", model);
		}

		public ActionResult Edit(int id)
		{
			var model = Db.StaffPositions.First(x => x.Id == id);
			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(StaffPosition model)
		{
			//1. 判断职位名称是否重复
			if (Db.StaffPositions.Any(x => x.Id != model.Id && x.Name == model.Name))
			{
				return JError("职位名称冲突，请修改！");
			}

			if (model.Id == 0)
			{
				//添加
				Db.StaffPositions.Add(model);
			}
			else
			{
				//修改
				Db.Entry(model).State = EntityState.Modified;
			}
			Db.SaveChanges();
			return JSuccess("操作成功！", "tab_bsi_staffposition");
		}

		public ActionResult Delete(int id)
		{
			var model = new StaffPosition {Id = id};
			Db.Entry(model).State = EntityState.Deleted;
			Db.SaveChanges();
			return JSuccess("删除成功！", "tab_bsi_staffposition");
		}
	}
}